package com.googlecode.beanfiles.translators;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.googlecode.beanfiles.TranslationNotice;


/**
 * A translator that works with a single indexed value while translating properties.
 */
public class IndexedTranslator extends PropertyTranslator {

    public IndexedTranslator() { }
    
    /**
     * Subclasses and package-mates only.
     */
    protected IndexedTranslator(String propertyName) {
        super(propertyName);
    }
    
    
    public IndexedTranslator(String propertyName, int index) {
        super(propertyName);
        this.indexes.add(index);
    }

    protected static Log log() {
        return LogFactory.getLog(IndexedTranslator.class);
    }

    /**
     * Translates an indexed input value to it's target type.
     */
    @Override
    public Object read(Object i, Class<?> clazz) {
        return ((List<?>) i).get(indexes.get(0));
    }

    @Override
    public Object write(Object t) {
        return null;
    }
    
    protected void addNoticeIfNull(Object o) {
        if (o == null || StringUtils.trimToNull(o.toString()) == null) {
            addTranslationNotice(new TranslationNotice(indexes.get(0) + 1));
        }
    }

    public boolean resolvesProperty(String propertyName) {
        // can't say for sure.
        return false;
    }
    
    public List<Integer> getIndexes(String propertyName) {
        return indexes;
    }
    
}
